<?php
namespace Api\Controller;
use Think\Controller;

class MessageVerifyController extends Controller {
    
    //主帐号
    private $accountSid = '';
    //主帐号Token
    private $accountToken = '';
    //应用Id
    private $appId = '';
    //请求地址，格式如下，不需要写https://
    private $serverIP = '';
    //请求端口 
    private $serverPort = '';
    //REST版本号
    private $softVersion = '';
    
    public function _initialize(){

        $message_verify = C('MESSAGE_VERIFY');
        $this->accountSid   = $message_verify['accountSid'];
        $this->accountToken = $message_verify['accountToken'];
        $this->appId        = $message_verify['appId'];
        $this->serverIP     = $message_verify['serverIP'];
        $this->serverPort   = $message_verify['serverPort'];
        $this->softVersion  = $message_verify['softVersion'];
    }
    /**
      * 发送模板短信
      * @param to 手机号码集合,用英文逗号分开
      * @param datas 内容数据 格式为数组 例如：array('验证码','验证时间')，如不需替换请填 null
      * @param $tempId 模板Id
      */       
    function sendTemplateSMS($to="", $datas=array(), $tempId="")
    {
        vendor("Mobile.REST");
        $rest = new \REST($this->serverIP,$this->serverPort,$this->softVersion);
        $rest->setAccount($this->accountSid,$this->accountToken);
        $rest->setAppId($this->appId);

        // 发送模板短信
//        echo "Sending TemplateSMS to $to <br/>";
        $result = $rest->sendTemplateSMS($to,$datas,$tempId);
        if($result == NULL ) {
            echo "result error!";
            exit;
        }
        if($result->statusCode!=0) {
//            echo $tempId."<br>";
//            echo "error code :" . $result->statusCode . "<br>";
//            echo "error msg :" . $result->statusMsg . "<br>";
            //TODO 添加错误处理逻辑
            return array("code" => 0, "msg" => $result->statusMsg);
        }else{
//            echo "Sendind TemplateSMS success!<br/>";
            // 获取返回信息
//            $smsmessage = $result->TemplateSMS;
//            echo "dateCreated:".$smsmessage->dateCreated."<br/>";
//            echo "smsMessageSid:".$smsmessage->smsMessageSid."<br/>";
            return array("code" => 1, "msg" => "发送成功！");
            //TODO 添加成功处理逻辑
        }
    }
}